package com.mw.plp.uim;

import android.app.Application;
import android.os.Build;

import androidx.annotation.NonNull;

import com.hyphenate.util.EMLog;
import com.mw.plp.uim.common.interfaceOrImplement.UserActivityLifecycleCallbacks;
import com.mw.plp.uim.common.utils.PreferenceManager;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class DemoApplication implements Thread.UncaughtExceptionHandler{
    private static DemoApplication instance;
    public Application context;
    private UserActivityLifecycleCallbacks mLifecycleCallbacks = new UserActivityLifecycleCallbacks();

    public static DemoApplication getInstance() {
        if (instance==null){
            instance=new DemoApplication();
        }
        return instance;
    }
    public void withInit(Application context){
        this.context=context;
        initThrowableHandler();
        // 初始化PreferenceManager
        PreferenceManager.init(context);
        registerActivityLifecycleCallbacks();
        closeAndroidPDialog();
        //todo 隐私正常
        initSDK();
    }

    private void initThrowableHandler() {
       // Thread.setDefaultUncaughtExceptionHandler(this);
    }

    private void initHx() {
        // init hx sdk
        EMLog.i("DemoApplication", "application initHx");
        DemoHelper.getInstance().init(context);
    }

    public void initSDK() {
        initHx();
    }

    private void registerActivityLifecycleCallbacks() {
        context.registerActivityLifecycleCallbacks(mLifecycleCallbacks);
    }



    public UserActivityLifecycleCallbacks getLifecycleCallbacks() {
        return mLifecycleCallbacks;
    }
    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
        EMLog.e("demoApp", e.getMessage());

    }

    /**
     * 解决androidP 第一次打开程序出现莫名弹窗
     * 弹窗内容“detected problems with api ”
     */
    private void closeAndroidPDialog(){
        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
            try {
                Class aClass = Class.forName("android.content.pm.PackageParser$Package");
                Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
                declaredConstructor.setAccessible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Class cls = Class.forName("android.app.ActivityThread");
                Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
                declaredMethod.setAccessible(true);
                Object activityThread = declaredMethod.invoke(null);
                Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
                mHiddenApiWarningShown.setAccessible(true);
                mHiddenApiWarningShown.setBoolean(activityThread, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
